package com.guoguo.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.guoguo.base.BaseController;
import com.guoguo.base.PageBean;
import com.guoguo.base.PaginationContext;
import com.guoguo.entity.Album;
import com.guoguo.entity.Model;
import com.guoguo.entity.SysImg;
import com.guoguo.entity.vo.AlbumVO;
import com.guoguo.entity.vo.SysImgVO;
import com.guoguo.enums.AliOssImageEnum;
import com.guoguo.service.AlbumService;
import com.guoguo.service.SysImgService;
import com.guoguo.service.UploadService;
import com.guoguo.utils.AliOssUtils;
import com.guoguo.utils.FileUtils;
import com.guoguo.utils.StringUtils;
import com.guoguo.utils.UUIDUtils;
import com.guoguo.utils.aliOss.AliOssImageConfig;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Logger;

/**
 * Created by Administrator on 2017/9/12 0012.
 */
@Controller
@RequestMapping("/view")
public class SysImgController extends BaseController {
    private final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(SysImgController.class);

    @Autowired
    private SysImgService sysImgService;
    @Autowired

    private UploadService uploadService;
    @Autowired
    private AlbumService albumService;


    @RequestMapping(value = "/sysImg", method = RequestMethod.GET)
    public ModelAndView selectList(@RequestParam int pageNumber, @RequestParam int pageSize) {

        Map<String, Object> data = new HashedMap();
        PageBean<SysImgVO> sysImgList = sysImgService.sysImgList(pageNumber, pageSize);
        data.put("sysImgList", sysImgList);
        return returnView("/sysImg", data);
    }


    /**
     * 去跳转修改页面
     *
     * @param sysId
     * @return
     */
    @RequestMapping(value = "/toUpdateSysImg", method = RequestMethod.GET)
    public ModelAndView selectSysImgById(String sysId) {
        Map<String, Object> data = new HashedMap();
//        AliOssImageConfig aliOssImageConfig = new AliOssImageConfig("test");
        List<Album> album = albumService.getAlbum();
        Map<Integer, String> sysImgs = new TreeMap<>();
        sysImgs.put(0, "启动图");
        sysImgs.put(1, "轮播图");
        sysImgs.put(2, "闪图");
        sysImgs.put(3, "水印图");
        data.put("imgTypeList", sysImgs);
        data.put("albumList", album);
        if ("".equals(sysId) || sysId == null) {
            data.put("sysId", UUIDUtils.getUUID());

        } else {
            SysImg sysImg = sysImgService.selectByPrimaryKey(sysId);
            data.put("sysId", sysImg.getSysId());
            data.put("sysImgType", sysImg.getSysImgType());
            data.put("sysImgUrl", sysImg.getSysImgUrl());
            data.put("sysImgUrlLocal", AliOssUtils.getImageUrl(sysImg.getSysImgUrl()));//, aliOssImageConfig));
            data.put("createTime", sysImg.getCreateTime());
            data.put("args1", sysImg.getArgs1());
        }
        return returnView("/sysImg/sysImage", data);
    }

    //添加/修改系统图
    @ResponseBody
    @RequestMapping(value = "/updateSysImg", method = RequestMethod.POST)
    public JSONObject updateSysImg(@RequestParam String sysId, @RequestParam int sysImgType, @RequestParam Boolean isShow, @RequestParam String args1, @RequestParam String sysImgUrl) {

        SysImg sysImg = sysImgService.addSysImg(sysId, sysImgType, isShow, args1, sysImgUrl, new Date(), new Date());
        return returnResult(200, sysImg, "");
    }

    /**
     * 根据Id删除
     *
     * @param sysId
     * @return
     */
    @RequestMapping(value = "/deleteSysImg", method = RequestMethod.GET)
    public ModelAndView deleteSysImg(String sysId) {
        sysImgService.deleteById(sysId);
        Map<String, Object> data = new HashedMap();
        PageBean<SysImgVO> sysImgList = sysImgService.sysImgList(PaginationContext.getPageNum(), PaginationContext.getPageSize());
        data.put("sysImgList", sysImgList);
        return returnView("/sysImg", data);
    }

    /**
     * 图片上传
     *
     * @param fileName
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/uploadSysImg", method = RequestMethod.POST)
    public JSONObject uploadFile(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile fileName, @RequestParam int isZip) {

        List<String> list = FileUtils.springFileUpload(request, isZip);
        List<String> ossFile = uploadService.uploadFile(list);
        JSONObject jsonObject = new JSONObject();
//        AliOssImageConfig aliOssImageConfig = new AliOssImageConfig("test");
        if (ossFile.size() > 0) {
            jsonObject.put("sysImgUrl", AliOssUtils.getImageUrl(ossFile.get(0)));//, aliOssImageConfig));
            jsonObject.put("sysImgUrlLocal", ossFile.get(0));
            logger.debug("=======>>>   ossFile path:" + ossFile.get(0));
        }
        return returnResult(200, jsonObject, "");
    }

}
